package cn.dnui_lf_705.dnui_16110100705.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.ShopBean;
import cn.dnui_lf_705.dnui_16110100705.controller.FoodListActivity;
import cn.dnui_lf_705.dnui_16110100705.model.Constant;


public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<ShopBean> mDatalist;
    private LayoutInflater mLayoutInflater;
    private OnRecyclerViewItemClickListener monItemClickListener=null;
    //构造方法
    public ShopAdapter(Context mContext, List mDatalist){
        this.mContext = mContext;
        this.mDatalist = mDatalist;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @NonNull
    @Override
    //加载布局文件
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        v.setOnClickListener(this); //条目布局添加点击事件
        return new ViewHolder(v);
    }
//继承recycleview.viewholder 获取view控件
    public class ViewHolder extends RecyclerView.ViewHolder{
        RatingBar res_bar;
        TextView res_name,res_address;
        ImageView image;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            res_name = itemView.findViewById(R.id.res_name);
            res_address = itemView.findViewById(R.id.res_address);
            res_bar = itemView.findViewById(R.id.ratingBar);
            image = itemView.findViewById(R.id.res_image);
        }
    }
    //绑定对应数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, final int i) {
            final ShopBean entity = mDatalist.get(i);
            if(null == entity)
                return;
            ViewHolder viewHolder = (ViewHolder) Holder;
            viewHolder.res_name.setText(entity.getShopname());
            viewHolder.res_address.setText(entity.getAddress());
            viewHolder.res_bar.setRating(entity.getLevel());
            Picasso.with(mContext).load(Constant.BASE_URL+entity.getPic()).into(viewHolder.image);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到店铺页面
                    String str = String.valueOf(mDatalist.get(i).getShop_id());
                    String shopname = String.valueOf(mDatalist.get(i).getShopname());
                    String phonenum = String.valueOf(mDatalist.get(i).getPhonenum());
                    Intent intent = new Intent(mContext,FoodListActivity.class);
                    intent.putExtra("shop_id",str);
                    intent.putExtra("shop_name",shopname);
                    intent.putExtra("phonenum",entity.getPhonenum());
                    mContext.startActivity(intent);
                }
            });
    }


    private AdapterView.OnItemClickListener mOnItemClickListener;
    private AdapterView.OnItemLongClickListener mOnItemLongClickListener;

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    interface OnItemClickListener {
        void onClick(RecyclerView.ViewHolder VH, int position);
    }

    interface OnItemLongClickListener {
        void onLongClick(RecyclerView.ViewHolder VH, int position);
    }



//获取数据大小
    @Override
    public int getItemCount() {
        return mDatalist.size();
    }

    public interface OnRecyclerViewItemClickListener{
        void onItemClick(View view, Object data);
    }
//外部获取点击事件
    @Override
    public void onClick(View v) {
        if(monItemClickListener!=null){
            //用getTag获取数据
            monItemClickListener.onItemClick(v,v.getTag());
        }
    }




    public void setOItemClickListener(OnRecyclerViewItemClickListener listener) {
       this.monItemClickListener=listener;
    }

}
